package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity.SysUserPermission;
import cn.wgx.modules.sys.services.SysUserPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Api(tags = "3.3 用户特殊授权 - 用户特殊授予权限")
@RestController
@RequestMapping(path = "api/userPer")
public class SysUserPerController extends BaseController{

    @Autowired
    SysUserPermissionService sysUserPermissionService;

    @ApiOperation("获取用户已经被特殊授权的权限列表")
    @ApiPermission(value = false)
    @RequestMapping(path = "getUserPerUserId", method = RequestMethod.GET)
    public Map<String,Object> getPer(Integer user_id) {
        SysUserPermission sysUserPermission= new SysUserPermission();
        sysUserPermission.setUserId(user_id);
        return returnData(sysUserPermissionService.getList(sysUserPermission, true));
    }


    @ApiOperation(value = "特殊授权 -> 添加/删除权限, 一对多", notes = "用户独立授权,不收其他角色职责限制,每次请求将会删除上次授权的全部权限")
    @ApiPermission
    @RequestMapping(path = "updatePermission", method = RequestMethod.PATCH)
    public Map<String,Object> updateRolePermission(@ApiParam(value = "SysDutyPermission对象数组,只需userId和permissionId;  数组形式,例如: [{userId:1, permissionId:1}]") @RequestBody List<SysUserPermission> sysDutyPermissions) {
        if (sysDutyPermissions == null || sysDutyPermissions.size() < 0) {
            return returnErrorMap("参数错误");
        }
        Integer n = 0;
        n = sysUserPermissionService.updateUserPermission(sysDutyPermissions);
        return returnData(n);
    }
}